package com.project.controller.gradeController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.project.bean.GradeBean;
import com.project.bean.RoleBean;
import com.project.service.gradeService.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @RequestMapping("findAllRole")
    public List<RoleBean> findAllRole(){
        return roleService.findAllRole();
    }
    @RequestMapping("findAll")
    public PageInfo<RoleBean> findAll(int pageNO){
        return roleService.findAll(pageNO);
    }

    @RequestMapping("add")
    public String add(@Valid RoleBean roleBean, BindingResult result, int[] gradeId) throws Exception {
        if(result.hasErrors()){
            return new ObjectMapper().writeValueAsString(result.getFieldErrors());
        }
        roleService.add(roleBean, gradeId);
        return "ok";
    }

    @RequestMapping("del")
    public void del(int id){
        roleService.del(id);
    }

    @RequestMapping("findById")
    public RoleBean findById(int id){
        return roleService.findById(id);
    }

    @RequestMapping("update")
    public void update(int id,int[] gradeId){
        roleService.update(id, gradeId);
    }

    @RequestMapping("findGrade")
    public List<GradeBean> findGrade(int id, int[] roleId){
        return roleService.findGrade(roleId);
    }
}
